package webmote.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.SourcesMouseEvents;

public class ScreenPanel extends AbsolutePanel implements SourcesMouseEvents {

    private Label eventsCatcher;
    private Grid image;

    public ScreenPanel() {
        image = new Grid();
        image.setCellSpacing(0);
        image.setCellPadding(0);
        add(image, 0, 0);
        
        eventsCatcher = new Label();
        add(eventsCatcher, 0, 0);
    }

    public void setTilesIds(int[][] tiles) {
        image.resize(tiles.length, tiles[0].length);
        String urlBase = GWT.getModuleBaseURL() + "tiles?id=";
        for (int row = 0; row < tiles.length; row++) {
            for (int col = 0; col < tiles[row].length; col++) {
                Image tileImage = (Image) image.getWidget(row, col);
                if (tileImage == null) {
                    tileImage = new Image();
                    tileImage.setSize(Constants.TILE_SIZE + "px", Constants.TILE_SIZE + "px");
                    image.setWidget(row, col, tileImage);
                }
                tileImage.setUrl(urlBase + tiles[row][col]);
            }
        }
    }

    @Override
    public void setWidth(String width) {
        super.setWidth(width);
        eventsCatcher.setWidth(width);
        image.setWidth(width);
    }

    @Override
    public void setHeight(String height) {
        super.setHeight(height);
        eventsCatcher.setHeight(height);
        image.setHeight(height);
    }

    public void addMouseListener(MouseListener listener) {
        eventsCatcher.addMouseListener(listener);
    }

    public void removeMouseListener(MouseListener listener) {
        eventsCatcher.removeMouseListener(listener);
    }

}
